package cn.rongdao.jrkabao.widget;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.mifengkong.frtools.inputmethod.FRView;
import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frwidget.popup_window.base.SlideFromBottomBasePopupMenu;

import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.CreditAndDepositCardBean;
import cn.rongdao.jrkabao.ui.cashadvance.adapter.DialogSelectCardAdapter;
import cn.rongdao.jrkabao.ui.cashadvance.adapter.DialogSelectCardViewHolder;
import cn.rongdao.jrkabao.ui.cashadvance.addcard.AddCreditCardForCashActivity;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.DialogFromBottomHeader;
import cn.rongdao.jrkabao.ui.main.mainfragment.loanfragment.uicomponent.CheckAllProductUIComponent;

/**
 * Created by jack on 2017/9/12
 */

public class SelectCreditCardFromBottom implements FRBaseRecyclerAdapter.OnItemClickListener<DialogSelectCardViewHolder> {

    private SlideFromBottomBasePopupMenu mSlideFromBottomBasePopupMenu;

    private DialogSelectCardAdapter selectCardAdapter;
    private RecyclerAdapterWithHF mAdapter;

    private OnDialogSelectCardClickListener onDialogSelectCardClickListener;

    private CheckAllProductUIComponent viewFooter;

    public SelectCreditCardFromBottom(final Activity activity) {
        View itemView = FRView.inflateLayout(activity, R.layout.dialog_setect_credit_card);
        DialogFromBottomHeader dialogFromBottomHeader = itemView.findViewById(R.id.dscc_select_card);
        RecyclerView recyclerView = itemView.findViewById(R.id.dscc_recyclerview);
        viewFooter = FRView.inflateLayout(activity, R.layout.layout_loan_footer_view);

        mSlideFromBottomBasePopupMenu = new SlideFromBottomBasePopupMenu(activity);
        mSlideFromBottomBasePopupMenu.setMenuItems(itemView);

        selectCardAdapter = new DialogSelectCardAdapter(activity, this);
        mAdapter = new RecyclerAdapterWithHF(selectCardAdapter);
        mAdapter.addFooter(viewFooter);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.setAdapter(mAdapter);

        dialogFromBottomHeader.setNameAndOnClickListener("选择付款方式", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        viewFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                AddCreditCardForCashActivity.start(activity);
            }
        });
    }

    public void setAdapterDataList(List<CreditAndDepositCardBean> list) {
        if (null != selectCardAdapter && null != mAdapter) {
            selectCardAdapter.setDataList(list);
            viewFooter.setPayWayContent("+ 添加新的信用卡");
            mAdapter.notifyDataSetChanged();
        }
    }

    public void dismiss() {
        if (null != mSlideFromBottomBasePopupMenu) {
            mSlideFromBottomBasePopupMenu.dismiss();
        }
    }

    public void show() {
        if (null != mSlideFromBottomBasePopupMenu) {
            mSlideFromBottomBasePopupMenu.showPopupWindow();
        }
    }

    public interface OnDialogSelectCardClickListener {
        void onItemClick(int position);
    }

    public void setOnDialogSelectCardClickListener(OnDialogSelectCardClickListener onDialogSelectCardClickListener) {
        this.onDialogSelectCardClickListener = onDialogSelectCardClickListener;
    }

    @Override
    public void onItemClick(DialogSelectCardViewHolder dialogSelectCardViewHolder, int position) {
        if (null != onDialogSelectCardClickListener) {
            onDialogSelectCardClickListener.onItemClick(position);
        }
    }
}